package com.dg.gaming.client;

import java.awt.*;
import java.util.*;

import javax.swing.*;

import com.dg.gaming.api.common.*;
import com.dg.gaming.api.common.util.*;
import com.dg.gaming.client.gui.*;
import nanoxml.*;


public abstract class DGGameTable extends JFrame implements IClient,
        Observer, IGameClientGUI, IGameGUI {
    private static final int FRAME_HEIGHT = 435;
    private static final int FRAME_WIDTH = 590;
    // Declare constants
    private static final int WIDTH = 600;
    private static final int HEIGHT = 500;

    private int width = WIDTH;
    private int height = HEIGHT;

    private static final int SPACING = 5;
    private static final Font GAME_OVER_FONT = new Font("Arial", Font.BOLD, 20);
    private static final Color GAME_OVER_COLOUR = new Color(200, 0, 0);

    private String title; // title of this frame

    protected TableConnection conn = null; // Connection
    private TableConnectionPanel connectionPanel = null;
    private DGClientPanel clientPanle;
    /**
     * Constructor to a standard DGGameFrame.  NOTE: Every contructor of a
     * sub class of this must call the setMVC (model, view, controller) method.
     *
     * @param conn	 Client connection thread which holds the link to the
     *               server, user and table lists.
     * @param width  Width of the table frame.
     * @param height Height of the table frame.
     */
    public DGGameTable() {
        this(WIDTH, HEIGHT);
    }


    public DGGameTable(int width, int height) {
        super();
        // Set fields

        this.width = width;
        this.height = height;

        // Load images (in seperate thread)
        GameImages.loadImages();

        // Set the title of the client window.
        this.setTitle(GameLabels.getClientTitle());

        // Inform game properties this isn't an applet (i.e. is an application)
        DGUtils.setApplet(false);

        // Set size, move frame to center and make visible
        Dimension size = DGAwt.getMinimumSize(this, FRAME_WIDTH,
                                              FRAME_HEIGHT);
        this.setSize(size);
        Point location = DGAwt.getCentredLocation(this, size);
        this.setLocation(location.x, location.y);
        // Set convience fields
        connectionPanel = new TableConnectionPanel(this);
        getContentPane().add(connectionPanel);

        // Set background colour of frame
        getContentPane().setBackground(GameProperties.getBackgroundColour());

        while (!GameImages.isFinishedLoading()) {}
        setIconImage(GameImages.getImage("game.icon"));

        // Make panel visible
        setVisible(true);
        validate();

    }


    /**
     * Set the link to the table frames hash in the DGClientFrame (must
     * be deleted when the client is removed).
     *
     * @param tableFramesHash
     */
    public void setTableFramesHash(HashMap tableFramesHash) {
        clientPanle.setTableFramesHash(tableFramesHash);
    }

    /**
     * Player leaves the table.
     */



    /**
     * Implementation of the ITable interface.
     *
     * @see com.dg.gaming.client.gui.ITable#receiveTableMessage(nanoxml.XMLElement)
     */
    public void receiveTableMessage(XMLElement message, String tableNum) {
        clientPanle.receiveTableMessage(message);
        receiveMessage(message);
    }


    public void setUITitle(String title) {
    }

    public ChatPrivateDialog getChatPrivateDialog(long usernameTo,
                                                  AbstractConnectionThread conn) {
        return null;
    }

    public void getPropertyDialog(AbstractConnectionThread conn) {
    }

    public void getUserDialog(User user) {
    }

    public void getRulesDialog() {
    }

    public boolean hasExtendedInfo() {
        return false;
    }

    public String getExtendedTableInfoString(Table theTable) {
        return "";
    }

    public abstract GameModel getGameModel();

    public void setupMVC(GameModel model, DGComponent mainView,
                         GameController controller) {
        clientPanle.setupMVC(model, mainView, controller);
    }

    protected void receiveMessage(XMLElement message) {}

    public void receiveGameMessage(XMLElement message) {
        receiveMessage(message);
        receiveTableMessage(message, "");
    }

    public void connectOK(AbstractConnectionThread conn) {
        TableConnection cblCon = (TableConnection) conn;
        // Update client
        this.conn = cblCon;

        cblCon.setClientInterface(this);

        // Create new dg client frame

        getContentPane().remove(connectionPanel);
        clientPanle=new DGClientPanel(this,cblCon);
         getContentPane().setLayout(new BorderLayout());
        getContentPane().add(clientPanle,BorderLayout.CENTER);
        this.title = GameLabels.getTableTitle() + " " +
                     cblCon.getTableNum() + " - " +
                     DGLabels.getInstance().get("player") + ": " +
                     cblCon.getUserName();


        // Set title of frame
        setTitle(title);

        // Set up GUI


        // Set image icon
        setIconImage(GameImages.getImage("game.icon"));

        // Add listeners


        // Set size of the frame
        if (width != -1 && height != -1) {
            setSize(new Dimension(width, height));
        }
        validate();
        repaint();
    }
    public void update(Observable obs, Object obj) {
           clientPanle.update(obs,obj);
    }
    public void setGamePanel(DGPanel gamePanel) {
        clientPanle.setGamePanel(gamePanel);
    }
}
